﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DinhMuc.BUL;
using DinhMuc.Public;

namespace DinhMuc.UI.Frm_Form
{
    public partial class TKSX_NVLADD : Form
    {
        public TKSX_NVLADD()
        {
            InitializeComponent();
        }
        DataTable dtPublic = new DataTable();
        int indexRow = 0;
        private void TKSX_NVLADD_Load(object sender, EventArgs e)
        {
            indexRow = 0;
            dsTKSX_TP();
            dtPublic.Columns.Add("ID");
            dtPublic.Columns.Add("ID_SX_TP");
            dtPublic.Columns.Add("soDH");
            dtPublic.Columns.Add("maNVL");
            dtPublic.Columns.Add("DVT");
            dtPublic.Columns.Add("tenTP");
            dtPublic.Columns.Add("ngay");
            dtPublic.Columns.Add("ca");
            dtPublic.Columns.Add("phuTrach");
            dtPublic.Columns.Add("dmThoi");
            dtPublic.Columns.Add("thoiTT");
            dtPublic.Columns.Add("dmTrangL1");
            dtPublic.Columns.Add("trangL1TT");
            dtPublic.Columns.Add("dmTrangL2");
            dtPublic.Columns.Add("trangL2TT");
            dtPublic.Columns.Add("dmIn");
            dtPublic.Columns.Add("inTT");
            dtPublic.Columns.Add("dmChiaCuon");
            dtPublic.Columns.Add("chiaCuonTT");
            dtPublic.Columns.Add("dmCatDan");
            dtPublic.Columns.Add("catDanTT");
        }
        private void dsTKSX_TP()
        {
            tblTKSX_TPBUL tp = new tblTKSX_TPBUL();
            gridEX1.DropDowns["cbTKSX_TP"].DataSource = tp.bangTKSX();
        }

        private void gridEX1_CellValueChanged(object sender, Janus.Windows.GridEX.ColumnActionEventArgs e)
        {
            try
            {
                if (e.Column.Key == "soDH")
                {
                    tblTKSX_TPBUL tp = new tblTKSX_TPBUL();
                    tblDinhMucBUL dm = new tblDinhMucBUL();
                    DataTable dt = tp.bangTKSX(int.Parse(gridEX1.GetValue("soDH").ToString()));
                    if (dt.Rows.Count > 0)
                    {
                        DataRow row = dt.Rows[0];
                        gridEX1.SetValue("ID_SX_TP", row["ID_SX"].ToString());
                        //viet combobox nvl . de chon nvl theo so DH
                        gridEX1.DropDowns["dsNVL_bySoDH"].DataSource = dm.dsNVL_bysoDH(int.Parse(row["soDH"].ToString()));
                        //----------
                        gridEX1.SetValue("soDH", row["soDH"].ToString());
                        gridEX1.SetValue("tenTP", row["maNVL"].ToString());
                        gridEX1.SetValue("ngay", row["ngay"].ToString());
                        gridEX1.SetValue("ca", row["ca"].ToString());
                        gridEX1.SetValue("phuTrach", row["phuTrach"].ToString());
                    }
                }
                if (e.Column.Key == "maNVL")
                {
                    //tinh toan dmNVL theo tam suat 
                    tblTKSX_TPBUL tp = new tblTKSX_TPBUL();
                    tblDinhMucBUL dm = new tblDinhMucBUL();
                    DataTable dt = tp.bangTKSX(int.Parse(gridEX1.GetValue("ID_SX_TP").ToString()));
                    DataTable dt1 = dm.dmNVL_bysoDH_NVL(int.Parse(gridEX1.GetValue("soDH").ToString()), gridEX1.GetValue("maNVL").ToString());
                    if (dt.Rows.Count > 0 && dt1.Rows.Count > 0)
                    {
                        DataRow row = dt.Rows[0];
                        DataRow row1 = dt1.Rows[0];
                        gridEX1.SetValue("DVT", row1["DVT"].ToString());
                        //tinh dmnvl_thoi
                        if (float.Parse("0" + row["dmThoi"].ToString()) != 0)
                        {
                            float dmThoi1 = float.Parse("0" + row["thoiTT"].ToString()) / float.Parse("0" + row["dmThoi"].ToString());
                            float dmThoi2 = float.Parse("0" + row1["dmThoi"].ToString());
                            gridEX1.SetValue("dmThoi", dmThoi1 * dmThoi2);
                        }
                        else
                            gridEX1.SetValue("dmThoi", 0);
                        //dmNVL_trangl1
                        if (float.Parse("0" + row["dmTrangL1"].ToString()) != 0)
                        {
                            float dmTrangL11 = float.Parse("0" + row["trangL1TT"].ToString()) / float.Parse("0" + row["dmTrangL1"].ToString());
                            float dmTrangL12 = float.Parse("0" + row1["dmTrangL1"].ToString());
                            gridEX1.SetValue("dmTrangL1", dmTrangL11 * dmTrangL12);
                        }
                        else
                            gridEX1.SetValue("dmTrangL1", 0);
                        //dmNVL_trangl2
                        if (float.Parse("0" + row["dmTrangL2"].ToString()) != 0)
                        {
                            float dmTrangL21 = float.Parse("0" + row["trangL2TT"].ToString()) / float.Parse("0" + row["dmTrangL2"].ToString());
                            float dmTrangL22 = float.Parse("0" + row1["dmTrangL2"].ToString());
                            gridEX1.SetValue("dmTrangL2", dmTrangL21 * dmTrangL22);
                        }
                        else
                            gridEX1.SetValue("dmTrangL2", 0);

                        //dmNVL_in
                        if (float.Parse("0" + row["dmIn"].ToString()) != 0)
                        {
                            float dmIn1 = float.Parse("0" + row["inTT"].ToString()) / float.Parse("0" + row["dmIn"].ToString());
                            float dmIn2 = float.Parse("0" + row1["dmIn"].ToString());
                            gridEX1.SetValue("dmIn", dmIn1 * dmIn2);
                        }
                        else
                            gridEX1.SetValue("dmIn", 0);
                        //dmNVL_chiaCUon
                        if (float.Parse("0" + row["dmChiaCuon"].ToString()) != 0)
                        {
                            float dmChiaCuon1 = float.Parse("0" + row["chiaCuonTT"].ToString()) / float.Parse("0" + row["dmChiaCuon"].ToString());
                            float dmChiaCuon2 = float.Parse("0" + row1["dmChiaCuon"].ToString());
                            gridEX1.SetValue("dmChiaCuon", dmChiaCuon1 * dmChiaCuon2);
                        }
                        else
                            gridEX1.SetValue("dmChiaCuon", 0);
                        //dmNVL_catdan
                        if (float.Parse("0" + row["dmCatDan"].ToString()) != 0)
                        {
                            float dmCatDan1 = float.Parse("0" + row["catDanTT"].ToString()) / float.Parse("0" + row["dmCatDan"].ToString());
                            float dmCatDan2 = float.Parse("0" + row1["dmCatDan"].ToString());
                            gridEX1.SetValue("dmCatDan", dmCatDan1 * dmCatDan2);
                        }
                        else
                            gridEX1.SetValue("dmCatDan", 0);

                    }

                }
            }
            catch (Exception)
            {
            }
        }

        private void gridEX1_RecordAdded(object sender, EventArgs e)
        {
            indexRow++;
            DataRow row = dtPublic.NewRow();
            row["ID"] = indexRow;
            row["ID_SX_TP"] = gridEX1.GetValue("ID_SX_TP").ToString();
            row["soDH"] = gridEX1.GetValue("soDH").ToString();
            row["maNVL"] = gridEX1.GetValue("maNVL").ToString();
            row["DVT"] = gridEX1.GetValue("DVT").ToString();
            row["tenTP"] = gridEX1.GetValue("tenTP").ToString();
            row["ngay"] = gridEX1.GetValue("ngay").ToString();
            row["ca"] = gridEX1.GetValue("ca").ToString();
            row["phuTrach"] = gridEX1.GetValue("phuTrach").ToString();
            row["dmThoi"] = gridEX1.GetValue("dmThoi").ToString();
            row["thoiTT"] = gridEX1.GetValue("thoiTT").ToString();
            row["dmTrangL1"] = gridEX1.GetValue("dmTrangL1").ToString();
            row["trangL1TT"] = gridEX1.GetValue("trangL1TT").ToString();
            row["dmTrangL2"] = gridEX1.GetValue("dmTrangL2").ToString();
            row["trangL2TT"] = gridEX1.GetValue("trangL2TT").ToString();
            row["dmIn"] = gridEX1.GetValue("dmIn").ToString();
            row["inTT"] = gridEX1.GetValue("inTT").ToString();
            row["dmChiaCuon"] = gridEX1.GetValue("dmChiaCuon").ToString();
            row["chiaCuonTT"] = gridEX1.GetValue("chiaCuonTT").ToString();
            row["dmCatDan"] = gridEX1.GetValue("dmCatDan").ToString();
            row["catDanTT"] = gridEX1.GetValue("catDanTT").ToString();
            dtPublic.Rows.Add(row);        
            //tblTKSX_NVLBUL cls = new tblTKSX_NVLBUL();
            //tblTKSX_NVLPublic p = new tblTKSX_NVLPublic();
            //p.Id_SX_TP = int.Parse(gridEX1.GetValue("ID_SX_TP").ToString());
            //p.MaNVL = gridEX1.GetValue("maNVL").ToString();
            //p.DmThoi = float.Parse("0" + gridEX1.GetValue("dmThoi").ToString());
            //p.ThoiTT = float.Parse("0" + gridEX1.GetValue("thoiTT").ToString());
            //p.DmTrangL1 = float.Parse("0" + gridEX1.GetValue("dmTrangL1").ToString());
            //p.TrangL1TT = float.Parse("0" + gridEX1.GetValue("trangL1TT").ToString());
            //p.DmTrangL2 = float.Parse("0" + gridEX1.GetValue("dmTrangL2").ToString());
            //p.TrangL2TT = float.Parse("0" + gridEX1.GetValue("trangL2TT").ToString());
            //p.DmIn = float.Parse("0" + gridEX1.GetValue("dmIn").ToString());
            //p.InTT = float.Parse("0" + gridEX1.GetValue("inTT").ToString());
            //p.DmChiaCuon = float.Parse("0" + gridEX1.GetValue("dmChiaCuon").ToString());
            //p.ChiaCuonTT = float.Parse("0" + gridEX1.GetValue("chiaCuonTT").ToString());
            //p.DmCatDan = float.Parse("0" + gridEX1.GetValue("dmCatDan").ToString());
            //p.CatDanTT = float.Parse("0" + gridEX1.GetValue("catDanTT").ToString());

            //cls.themTKSX(p);
            //gridEX1.MoveToNewRecord();
            //TKSX_NVLADD_Load(sender, e);
        }
        private void insert()
        {
            for (int i = 0; i < dtPublic.Rows.Count; i++)
            {
                tblTKSX_NVLBUL cls = new tblTKSX_NVLBUL();
                DataRow row = dtPublic.Rows[i];
                tblTKSX_NVLPublic p = new tblTKSX_NVLPublic();
                p.Id_SX_TP = int.Parse(row["ID_SX_TP"].ToString());
                p.MaNVL = row["maNVL"].ToString();
                p.DmThoi = float.Parse("0" + row["dmThoi"].ToString());
                p.ThoiTT = float.Parse("0" + row["thoiTT"].ToString());
                p.DmTrangL1 = float.Parse("0" + row["dmTrangL1"].ToString());
                p.TrangL1TT = float.Parse("0" + row["trangL1TT"].ToString());
                p.DmTrangL2 = float.Parse("0" + row["dmTrangL2"].ToString());
                p.TrangL2TT = float.Parse("0" + row["trangL2TT"].ToString());
                p.DmIn = float.Parse("0" + row["dmIn"].ToString());
                p.InTT = float.Parse("0" + row["inTT"].ToString());
                p.DmChiaCuon = float.Parse("0" + row["dmChiaCuon"].ToString());
                p.ChiaCuonTT = float.Parse("0" + row["chiaCuonTT"].ToString());
                p.DmCatDan = float.Parse("0" + row["dmCatDan"].ToString());
                p.CatDanTT = float.Parse("0" + row["catDanTT"].ToString());

                cls.themTKSX(p);
                gridEX1.MoveToNewRecord();
                //TKSX_NVLADD_Load(sender, e);

            }
        }
        private void gridEX1_RecordUpdated(object sender, EventArgs e)
        {
            DataRow row = dtPublic.Rows[gridEX1.CurrentRow.RowIndex];
            row["ID_SX_TP"] = gridEX1.GetValue("ID_SX_TP").ToString();
            row["soDH"] = gridEX1.GetValue("soDH").ToString();
            row["maNVL"] = gridEX1.GetValue("maNVL").ToString();
            row["DVT"] = gridEX1.GetValue("DVT").ToString();
            row["tenTP"] = gridEX1.GetValue("tenTP").ToString();
            row["ngay"] = gridEX1.GetValue("ngay").ToString();
            row["ca"] = gridEX1.GetValue("ca").ToString();
            row["phuTrach"] = gridEX1.GetValue("phuTrach").ToString();
            row["dmThoi"] = gridEX1.GetValue("dmThoi").ToString();
            row["thoiTT"] = gridEX1.GetValue("thoiTT").ToString();
            row["dmTrangL1"] = gridEX1.GetValue("dmTrangL1").ToString();
            row["trangL1TT"] = gridEX1.GetValue("trangL1TT").ToString();
            row["dmTrangL2"] = gridEX1.GetValue("dmTrangL2").ToString();
            row["trangL2TT"] = gridEX1.GetValue("trangL2TT").ToString();
            row["dmIn"] = gridEX1.GetValue("dmIn").ToString();
            row["inTT"] = gridEX1.GetValue("inTT").ToString();
            row["dmChiaCuon"] = gridEX1.GetValue("dmChiaCuon").ToString();
            row["chiaCuonTT"] = gridEX1.GetValue("chiaCuonTT").ToString();
            row["dmCatDan"] = gridEX1.GetValue("dmCatDan").ToString();
            row["catDanTT"] = gridEX1.GetValue("catDanTT").ToString();

            dtPublic.AcceptChanges();
            gridEX1.DataSource = dtPublic;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            insert();
            MessageBox.Show("Thêm thành công");
            this.Close();
           // TKSX_NVLADD_Load(sender, e);
        }
    }
}
